﻿' LibWowAPI
' by Ronald M. Clifford (roncli@roncli.com)
'
' This source code is released under the GNU Library General Public License (LGPL) Version 2.1.

Imports System
Imports System.Collections.ObjectModel
Imports System.Linq
Imports Newtonsoft.Json
Imports roncliProductions.LibWowAPI.Enums
Imports roncliProductions.LibWowAPI.Extensions

Namespace roncliProductions.LibWowAPI.Data.GuildAchievements

    ''' <summary>
    ''' A class that retrieves guild achievement information from the Blizzard WoW API.
    ''' </summary>
    ''' <remarks></remarks>
    Public Class GuildAchievements
        Inherits WowAPIData

        Private aAchievements As New Schema.achievements

#Region "WowAPIData Overrides"

#Region "Public Properties"

        ''' <summary>
        ''' The length of time the data should be cached for, defaulting to 30 days for guild achievement information.
        ''' </summary>
        ''' <value></value>
        ''' <returns></returns>
        ''' <remarks></remarks>
        Public Overrides Property CacheLength As New TimeSpan(30, 0, 0, 0)

#End Region

#Region "Protected Properties"

        Protected Overrides ReadOnly Property CacheKey As String
            Get
                Return "LibWowAPI.GuildAchievements"
            End Get
        End Property

        Protected Overrides ReadOnly Property URI As Uri
            Get
                Return New Uri("/api/wow/data/guild/achievements", UriKind.Relative)
            End Get
        End Property

#End Region

#End Region

#Region "Properties"

        Private colCategories As Collection(Of Category)
        ''' <summary>
        ''' A list of guild achievements, grouped by categories, as returned from the Blizzard WoW API.
        ''' </summary>
        ''' <value></value>
        ''' <returns></returns>
        ''' <remarks></remarks>
        Public ReadOnly Property Categories As Collection(Of Category)
            Get
                Return colCategories
            End Get
        End Property

#End Region

#Region "Methods"

        ''' <summary>
        ''' A default constructor to retrieve guild achievement information from the Blizzard WoW API.
        ''' </summary>
        ''' <remarks></remarks>
        Public Sub New()
            Load()
        End Sub

        ''' <summary>
        ''' Loads the guild achievements.
        ''' </summary>
        ''' <remarks></remarks>
        Public Sub Load()
            MyBase.Retrieve()
            aAchievements = CType(JsonConvert.DeserializeObject(Data, GetType(Schema.achievements)), Schema.achievements)
            colCategories = (
                From c In aAchievements.achievements
                Select New Category(
                    c.id,
                    If(c.categories.Count = 0, Nothing,
                        (
                            From sc In c.categories
                            Select New Category(
                                sc.id,
                                Nothing,
                                sc.name,
                                (
                                    From a In sc.achievements
                                    Select New Achievement(
                                        a.id,
                                        a.title,
                                        a.points,
                                        a.description,
                                        a.reward,
                                        If(a.rewardItem Is Nothing, Nothing,
                                            New RewardItem(
                                                a.rewardItem.id,
                                                a.rewardItem.name,
                                                a.rewardItem.icon,
                                                CType(a.rewardItem.quality, Quality)
                                                )
                                            )
                                        )
                                    ).ToCollection()
                                )
                            ).ToCollection()
                        ),
                    c.name,
                    (
                        From a In c.achievements
                        Select New Achievement(
                            a.id,
                            a.title,
                            a.points,
                            a.description,
                            a.reward,
                            If(a.rewardItem Is Nothing, Nothing,
                                New RewardItem(
                                    a.rewardItem.id,
                                    a.rewardItem.name,
                                    a.rewardItem.icon,
                                    CType(a.rewardItem.quality, Quality)
                                    )
                                )
                            )
                        ).ToCollection()
                    )
                ).ToCollection()
        End Sub

#End Region

    End Class

End Namespace
